home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 May / PersonalComputerWorld-May2008-CoverdiscCD.iso / Software / Full / Nero 7 / Installation / Cab / A97B5616.cab / ScrollingB86A6A67.js < prev    next >
Encoding:
JavaScript  |  2006-06-28  |  8.7 KB  |  231 lines

  1. // when you focus on an item, the browser will automatically scroll it into view. This file provides additional functionality.
  2.  
  3. // It enables the pg up/dn buttons, and it updates the buttons' positions when scrolling takes place (these positions are needed
  4.  
  5. // when you move the focus, in order to calculate which other buttons on the page are closest)
  6.  
  7.  
  8.  
  9. function pageUpDown(direction)
  10.  
  11. {
  12.  
  13.     // This function determines what to do if the user presses the Page Up (+) or Page Down (-) key
  14.  
  15.  
  16.  
  17.     // Call the getScrollParent function to find the scrollable parent object
  18.  
  19.     var oScrollParent = getScrollParent()
  20.  
  21.     if (oScrollParent == null)
  22.  
  23.     {
  24.  
  25.         return
  26.  
  27.     }
  28.  
  29.     // find correct button to focus on
  30.  
  31.     var oScrollToBtn = getScrollToButton(oScrollParent, direction);
  32.  
  33.     oCurFocus = oScrollToBtn;
  34.  
  35.     oCurFocus.focus();
  36.  
  37.     // check if scrolling has taken place, and update positions of scrollable elements if needed
  38.  
  39.     if (didScroll())
  40.  
  41.     {
  42.  
  43.         updateScrollPositions()
  44.  
  45.     }
  46.  
  47. }
  48.  
  49.  
  50.  
  51. function getScrollParent()
  52.  
  53. {
  54.  
  55.     /* For an item to scroll correctly when the user presses the Page Up (+) or Page Down (-) keys, it has to have a parent object that
  56.  
  57.     has its custom "MCScrollable" attribute set to "true". This function locates that parent object */
  58.  
  59.     // Start with the object that currently has focus ...
  60.  
  61.     var oTempObj = oCurFocus
  62.  
  63.     // ... then check its parent, grandparent, great-grandparent, and so on.
  64.  
  65.     for (i=0; i<5000; i++)
  66.  
  67.     {
  68.  
  69.          oTempObj = oTempObj.parentElement
  70.  
  71.         // Stop when you get to the scrollable parent element
  72.  
  73.         if (oTempObj.MCScrollable == "true")
  74.  
  75.         {
  76.  
  77.             return oTempObj
  78.  
  79.         }
  80.  
  81.         // If you get as high as the BODY tag without finding a scrollable parent element, return null
  82.  
  83.         if (oTempObj.tagName == "BODY")
  84.  
  85.         {
  86.  
  87.             return null
  88.  
  89.         }
  90.  
  91.     }
  92.  
  93. }
  94.  
  95.  
  96.  
  97. function getScrollToButton(oScrollParent, direction)
  98.  
  99. {
  100.  
  101.     // This function determines which item to focus on next when the user presses the Page Up (+) or Page Down (-) key
  102.  
  103.  
  104.  
  105.     // find top & bottom of scroll parent relative to top of pg (don't worry about 2 px offset)
  106.  
  107.     var oBottomVisibleBtn = oCurFocus
  108.  
  109.     var oTopVisibleBtn = null
  110.  
  111.     var oLowerBtn
  112.  
  113.     var oHigherBtn = null
  114.  
  115.     //var oScrollToBtn
  116.  
  117.     var oScrollParentPos = oScrollParent.getBoundingClientRect()
  118.  
  119.     var nScrollParentTop = oScrollParentPos.top
  120.  
  121.     var nScrollParentBottom = oScrollParentPos.bottom
  122.  
  123.     var nScrollParentHeight = (nScrollParentBottom - nScrollParentTop)
  124.  
  125.  
  126.  
  127.     // go through all focusable elements in order;
  128.  
  129.     for (i=0; i<oScrollParent.all.length; i++)
  130.  
  131.     {
  132.  
  133.         //identify object
  134.  
  135.         var obj = oScrollParent.all(i)
  136.  
  137.         // make sure object is focusable
  138.  
  139.         if (obj.MCFocusable == "true" && direction == "down")
  140.  
  141.         {
  142.  
  143.             //find top of object
  144.  
  145.             var nObjTop = obj.nTopPos;
  146.  
  147.             // for the lowest btn whose top is visible (by at least two pixels), assign bottomVisibleBtn to object
  148.  
  149.             if ((nObjTop + 2) < nScrollParentBottom)
  150.  
  151.             {
  152.  
  153.                 oBottomVisibleBtn = obj
  154.  
  155.             }
  156.  
  157.             // Find lowest button down to one Scroll Parent Height below bottom visible btn
  158.  
  159.             if (nObjTop < (nScrollParentBottom + nScrollParentHeight))
  160.  
  161.             {
  162.  
  163.                 oLowerBtn = obj
  164.  
  165.             }
  166.  
  167.         }
  168.  
  169.         if (obj.MCFocusable == "true" && direction == "up")
  170.  
  171.         {
  172.  
  173.             var nObjBottom = obj.getBoundingClientRect().bottom;
  174.  
  175.             // for the first btn that's even partly visible, assign topVisibleBtn to object
  176.  
  177.             if (nObjBottom > nScrollParentTop && oTopVisibleBtn == null)
  178.  
  179.             {
  180.  
  181.                 oTopVisibleBtn = obj
  182.  
  183.             }
  184.  
  185.             // Find first button that's within one Scroll Parent Height above top visible btn
  186.  
  187.             if (nObjBottom > (nScrollParentTop - nScrollParentHeight) && oHigherBtn == null)
  188.  
  189.             {
  190.  
  191.                 oHigherBtn = obj
  192.  
  193.             }
  194.  
  195.         }
  196.  
  197.     }
  198.  
  199.     if (direction == "up")
  200.  
  201.     {
  202.  
  203.         if (oTopVisibleBtn != oCurFocus)
  204.  
  205.         {
  206.  
  207.             // if focus is not already on bottom visible btn, move focus to there and end function
  208.  
  209.             return oTopVisibleBtn
  210.  
  211.         }
  212.  
  213.         else
  214.  
  215.         {
  216.  
  217.             // If focus is already on bottom visible, focus on button one box height lower
  218.  
  219.             return oHigherBtn
  220.  
  221.         }
  222.  
  223.     }
  224.  
  225.  
  226.  
  227.     if (direction == "down")
  228.  
  229.     {
  230.  
  231.         if (oBottomVisibleBtn != oCurFocus)
  232.  
  233.         {
  234.  
  235.             // if focus is not already on bottom visible btn, return bottom visible btn and end function
  236.  
  237.             return oBottomVisibleBtn
  238.  
  239.         }
  240.  
  241.         else
  242.  
  243.         {
  244.  
  245.             // If focus is already on bottom visible, return button one box height lower than that
  246.  
  247.             return oLowerBtn
  248.  
  249.         }
  250.  
  251.     }
  252.  
  253. }
  254.  
  255.  
  256.  
  257. function didScroll()
  258.  
  259. {
  260.  
  261.     // This function indicates whether current-focus item has scrolled or otherwise moved vertically on the page.
  262.  
  263.     // If the current-focus item's top position does not equal the position it was in when the page loaded, return true
  264.  
  265.     if((oCurFocus.getBoundingClientRect().top - 2) != oCurFocus.nTopPos)
  266.  
  267.     {
  268.  
  269.         return true
  270.  
  271.     }
  272.  
  273. }
  274.  
  275.  
  276.  
  277. function updateScrollPositions()
  278.  
  279. {
  280.  
  281.     // object has scrolled; reset "nCenterYCoord" property for all scrolling buttons in box
  282.  
  283.     // so that auto nav will be based on new positions for buttons
  284.  
  285.     var oScrollParent = getScrollParent()
  286.  
  287.     if (oScrollParent == null)
  288.  
  289.     {
  290.  
  291.         oScrollParent = body
  292.  
  293.     }
  294.  
  295.     // for all objects in the parent container of the scrolling element
  296.  
  297.     for (i=0; i<oScrollParent.all.length; i++)
  298.  
  299.     {
  300.  
  301.         //identify object
  302.  
  303.         var obj = oScrollParent.all(i)
  304.  
  305.         // make sure object is focusable
  306.  
  307.         if (obj.MCFocusable == "true")
  308.  
  309.         {
  310.  
  311.             // reassign Y coordinate values for based on its new top position
  312.  
  313.             var tempObjPosition = obj.getBoundingClientRect();
  314.  
  315.             obj.nTopPos = tempObjPosition.top - 2
  316.  
  317.             obj.nBottomPos = tempObjPosition.bottom - 2
  318.  
  319.             var objCenterTop = (obj.nTopPos+(obj.nHeight/2))
  320.  
  321.             obj.nCenterYCoord = objCenterTop
  322.  
  323.         }
  324.  
  325.     }
  326.  
  327. }
  328.  
  329.  
  330.  
  331. ///////////////////////////////////////////////////////////////////////////////////////////////////
  332.  
  333. // code for counter that indicates how many buttons are in scrolling menu, etc.
  334.  
  335.  
  336.  
  337. // variable to indicate whether there is a counter on the page
  338.  
  339. var isCounter = false
  340.  
  341. // variable to hold array of scrolling buttons
  342.  
  343. var aScrollBtnsArray
  344.  
  345.  
  346.  
  347. function setCounter()
  348.  
  349. {
  350.  
  351.     /* this function sets the numeric value for the item counter found at the lower right of
  352.  
  353.     each scrollable menu in the templates, to indicate how many focusable items are in the menu. */
  354.  
  355.     // Reset isCounter value to true, indicating that there is a counter on the page
  356.  
  357.     isCounter = true
  358.  
  359.     //Make array for scrolling buttons
  360.  
  361.     aScrollBtnsArray = new Array()
  362.  
  363.     // Loop through all focusable items in the scrolling span
  364.  
  365.     for (i=0; i<scrollspan.all.length; i++)
  366.  
  367.     {
  368.  
  369.         var obj = scrollspan.all(i)
  370.  
  371.         if(obj.MCFocusable == "true")
  372.  
  373.         {
  374.  
  375.             var nextElement = aScrollBtnsArray.length
  376.  
  377.             aScrollBtnsArray[nextElement] = obj
  378.  
  379.         }
  380.  
  381.     }
  382.  
  383.     // update the SPAN whose ID is "counterTotal" to reflect total number of focusable items in scrolling span
  384.  
  385.     counterTotal.innerHTML = aScrollBtnsArray.length
  386.  
  387. }
  388.  
  389.  
  390.  
  391. function updateCounter()
  392.  
  393. {
  394.  
  395.      /* this function updates the numeric value for the item counter found at the lower right of
  396.  
  397.     each scrollable menu in the templates, to indicate which item currently has focus. */
  398.  
  399.  
  400.  
  401.     if (isCounter != true)
  402.  
  403.     {
  404.  
  405.         // if there is no counter on the page, return
  406.  
  407.         return
  408.  
  409.     }
  410.  
  411.     // variable to track whether focus is in scrollable menu
  412.  
  413.     var bFocusInMenu = false
  414.  
  415.     //Loop through all focusable items in scrolling menu
  416.  
  417.     for (i=0; i<aScrollBtnsArray.length; i++)
  418.  
  419.     {
  420.  
  421.         // if item currently has focus ...
  422.  
  423.         if (aScrollBtnsArray[i] == oCurFocus)
  424.  
  425.         {
  426.  
  427.             // set counter number to show which item has focus
  428.  
  429.             counterNum.innerHTML = (i + 1);
  430.  
  431.             // update variable to indicate focus is in scrollable menu
  432.  
  433.             bFocusInMenu = true
  434.  
  435.         }
  436.  
  437.     }
  438.  
  439.     // if focus in in menu, make sure arrows are not grayed out.
  440.  
  441.     if (bFocusInMenu == true)
  442.  
  443.     {
  444.  
  445.         itemCounterSpan.style.filter = "none"
  446.  
  447.     }
  448.  
  449.     // Else gray out arrows to indicate disabled state.
  450.  
  451.     else
  452.  
  453.     {
  454.  
  455.         itemCounterSpan.style.filter = "alpha(opacity=50)"
  456.  
  457.     }
  458.  
  459. }
  460.  
  461.